//
//  ReadPageDot.m
//  KidReading
//
//  Created by telen on 16/1/14.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadPageDot.h"
#import "ConstDefine.h"

@implementation ReadPageDot
{
    UIScrollView* scrview;
}

- (void)updateToIndex:(NSUInteger)index
{
    [self removeAllSubViews];
    _pod = _pod > 0 ? _pod : 12;
    _wh_value = _wh_value > 0 ? _wh_value : 12;
    if (self.height < _wh_value) {
        self.height = _wh_value;
    }
    if (_count > 0 && index < _count) {
        int xy = -1;
        int xx = self.width;
        CGPoint pt = self.contentCenter;
        CGFloat pod = _pod;
        CGRect frame =  CGRectMake(0, 0, _wh_value, _wh_value);
        if (_readPageDotPt == ReadPageDot_left) {
            xx = (_wh_value*_count + pod*(_count-xy));
        }else if(_readPageDotPt == ReadPageDot_center){
            xx /= 2;
            xx += (_wh_value*_count + pod*(_count-xy))/2;
        }
        for (int i=0; i<_count; i++) {
            xx += pod*xy;
            pt.x = xx;
            if (index == _count-1-i) {
                if (_imageCurrent) {
                    UIImageView* imgv = [[UIImageView alloc] initWithFrame:frame];
                    imgv.image = _imageCurrent;
                    [self addSubview:imgv];
                    xx += imgv.width*xy;
                    imgv.center = pt;
                }else{
                    UIView* view = [[UIView alloc] initWithFrame:frame];
                    [self addSubview:view];
                    view.backgroundColor = [UIColor grayColor];
                    view.layer.cornerRadius = _wh_value*0.5;
                    [view.layer setMasksToBounds:YES];
                    xx += view.width*xy;
                    view.center = pt;
                }
            }else{
                if (_imageNormal) {
                    UIImageView* imgv = [[UIImageView alloc] initWithFrame:frame];
                    imgv.image = _imageNormal;
                    [self addSubview:imgv];
                    xx += imgv.width*xy;
                    imgv.center = pt;
                }else{
                    UIView* view = [[UIView alloc] initWithFrame:frame];
                    [self addSubview:view];
                    view.backgroundColor = [UIColor whiteColor];
                    view.layer.cornerRadius = _wh_value*0.5;
                    [view.layer setMasksToBounds:YES];
                    xx += view.width*xy;
                    view.center = pt;
                }
            }
        }
        _currentIndex = index;
    }else{
        _currentIndex = 0;
    }
}

- (void)cleanScrollView_KVO
{
    if (scrview) {
        [scrview removeObserver:self forKeyPath:@"contentOffset" context:nil];
        scrview = nil;
    }
}

- (void)autoChangeSign_For:(UIScrollView*)scrollview
{
    [self cleanScrollView_KVO];
    scrview = scrollview;
    [scrview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc
{
    [self cleanScrollView_KVO];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([@"contentOffset" isEqualToString:keyPath]) {
        CGPoint offset = scrview.contentOffset;
        long w = scrview.frame.size.width;
        long x = (long)offset.x + w/2;
        long index = x/w;
        if(index!=_currentIndex)[self updateToIndex:index];
    }
}

@end
